package cn.com.b2office.struts2.action;

import java.util.Properties;
import java.util.ResourceBundle;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import cn.com.b2office.domain.Member;
import cn.com.b2office.service.MemberService;
import cn.com.b2office.util.MD5;
import cn.com.b2office.util.RandomString;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author  zhenggang
 * @version Jul 15, 2008 4:08:56 PM
 * comment
 */
public class ForgetPasswordAction extends ActionSupport {

	private static final long serialVersionUID = 4591005681110842887L;

	private String mailContent;

	private String nameOrMemberNo;
	private String emailInput;   //收信地址，用户在页面输入的Email地址
	private String receiverName = ""; //收信人称呼
	private String memberPwd;
	private Properties propertie = new Properties();
	private MemberService memberService;
	
	public String execute() throws EmailException{
		//初始化密码
		String newPwd = RandomString.getRandomString(8);
		Member member = memberService.getMemberByEmail(emailInput);
		memberService.initializeMemberPwd(member.getMemberid(), MD5.hash(newPwd));
		
		//发送邮件通知
		mailContent = "您的初始化密码是："+newPwd+" ,请登录网站及时更改.";
		ResourceBundle bundle = ResourceBundle.getBundle("email");   //读取文件 email.properties
	
		SimpleEmail email = new SimpleEmail();   
        email.setHostName(bundle.getString("emailHostName"));  
        email.setAuthentication(bundle.getString("userName"), bundle.getString("passWord")); 
        email.addTo(emailInput,receiverName);   
        email.setFrom(bundle.getString("senderEmail"), bundle.getString("senderName"));
        email.setSubject(bundle.getString("setSubject"));  
        email.setCharset("UTF-8");   
        email.setMsg(mailContent); 
        email.send(); 
        
		return SUCCESS;
	}

	public void validateExecute(){
		
		if(nameOrMemberNo==null || nameOrMemberNo.replace(" ", "").equals("")){
			addFieldError("nameOrMemberNo", "用户名或会员号不能为空");
		}else if(memberService.getMemberByMemberno(nameOrMemberNo) == null && memberService.ifRegNameExist(nameOrMemberNo)==false){
			addFieldError("nameOrMemberNo", "用户名或会员号不存在");
		}
		
		if(emailInput==null || emailInput.replace(" ", "").equals("")){
			addFieldError("emailInput", "Email地址不能为空");
		}else if (memberService.ifRegEmailExist(emailInput) == false){
			addFieldError("emailInput", "Email地址不存在");
		}else if(memberService.getMemberByNameAndEmail(nameOrMemberNo, emailInput)==null && memberService.getMemberByMembernoAndEmail(nameOrMemberNo, emailInput)==null){
			addFieldError("emailInput", "Email地址与用户名或会员号不匹配");
		}
	}
	public void setEmailInput(String emailInput) {
		this.emailInput = emailInput;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setNameOrMemberNo(String nameOrMemberNo) {
		this.nameOrMemberNo = nameOrMemberNo;
	}

	public String getNameOrMemberNo() {
		return nameOrMemberNo;
	}

}
